package io.finer.erp.jeecg.bas.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;

import io.finer.erp.jeecg.bas.entity.ZrkPiecerate;
import io.finer.erp.jeecg.bas.mapper.ZrkPiecerateMapper;
import io.finer.erp.jeecg.bas.service.IZrkPiecerateService;

/**
 * @Description: 计件工资表
 * @Author: jeecg-boot
 * @Date:   2020-08-19
 * @Version: V1.0
 */
@Service
public class ZrkPiecerateServiceImpl extends ServiceImpl<ZrkPiecerateMapper, ZrkPiecerate> implements IZrkPiecerateService {

	@Autowired
	private ISysBaseAPI sysBaseAPI;
	
	@Override
	public IPage<ZrkPiecerate> getPageList(Page<ZrkPiecerate> page, ZrkPiecerate zrkPiecerate) {
		// TODO Auto-generated method stub
		LambdaQueryWrapper<ZrkPiecerate> queryWrapper = new LambdaQueryWrapper<>();
		if(StringUtils.isNotBlank(zrkPiecerate.getZrkType())) {
			queryWrapper.eq(ZrkPiecerate::getZrkType, zrkPiecerate.getZrkType());
		}
		if(StringUtils.isNotBlank(zrkPiecerate.getZrkJqbh())) {
			queryWrapper.eq(ZrkPiecerate::getZrkJqbh, zrkPiecerate.getZrkJqbh());
		}
		if(StringUtils.isNotBlank(zrkPiecerate.getCreateTime_begin())
				&& StringUtils.isNotBlank(zrkPiecerate.getCreateTime_end())) {
			queryWrapper.between(ZrkPiecerate::getCreateTime, zrkPiecerate.getCreateTime_begin(), zrkPiecerate.getCreateTime_end());
		}
		if(StringUtils.isNotBlank(zrkPiecerate.getZrkUserid())) {
			String[] userNames = StringUtils.split(zrkPiecerate.getZrkUserid(), ",");
			List<LoginUser> userList = sysBaseAPI.queryUserByNames(userNames);
			List<String> userIdList = Lists.newArrayList();
			if(userList!=null && userList.size()>0) {
				for(LoginUser user:userList) {
					userIdList.add(user.getId());
				}
				queryWrapper.in(ZrkPiecerate::getZrkUserid, userIdList);
			}
		}
		queryWrapper.orderByDesc(ZrkPiecerate::getCreateTime);
		return this.page(page, queryWrapper);
	}

	@Override
	public ModelAndView exportXls(HttpServletRequest request, ZrkPiecerate zrkPiecerate, Class<ZrkPiecerate> clazz,
			String title) {
		// TODO Auto-generated method stub
		// Step.1 组装查询条件
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        LambdaQueryWrapper<ZrkPiecerate> queryWrapper = new LambdaQueryWrapper<>();
		if(StringUtils.isNotBlank(zrkPiecerate.getZrkType())) {
			queryWrapper.eq(ZrkPiecerate::getZrkType, zrkPiecerate.getZrkType());
		}
		if(StringUtils.isNotBlank(zrkPiecerate.getZrkJqbh())) {
			queryWrapper.eq(ZrkPiecerate::getZrkJqbh, zrkPiecerate.getZrkJqbh());
		}
		if(StringUtils.isNotBlank(zrkPiecerate.getCreateTime_begin())
				&& StringUtils.isNotBlank(zrkPiecerate.getCreateTime_end())) {
			queryWrapper.between(ZrkPiecerate::getCreateTime, zrkPiecerate.getCreateTime_begin(), zrkPiecerate.getCreateTime_end());
		}
		if(StringUtils.isNotBlank(zrkPiecerate.getZrkUserid())) {
			String[] userNames = StringUtils.split(zrkPiecerate.getZrkUserid(), ",");
			List<LoginUser> userList = sysBaseAPI.queryUserByNames(userNames);
			List<String> userIdList = Lists.newArrayList();
			if(userList!=null && userList.size()>0) {
				for(LoginUser user:userList) {
					userIdList.add(user.getId());
				}
				queryWrapper.in(ZrkPiecerate::getZrkUserid, userIdList);
			}
		}

        // Step.2 获取导出数据
        List<ZrkPiecerate> pageList = this.list(queryWrapper);
        List<ZrkPiecerate> exportList = null;

        // 过滤选中数据
        String selections = request.getParameter("selections");
        if (oConvertUtils.isNotEmpty(selections)) {
            List<String> selectionList = Arrays.asList(selections.split(","));
            exportList = pageList.stream().filter(item -> selectionList.contains(getId(item))).collect(Collectors.toList());
        } else {
            exportList = pageList;
        }

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, title); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, clazz);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
	}
	
	/**
     * 获取对象ID
     *
     * @return
     */
    private String getId(ZrkPiecerate item) {
        try {
            return PropertyUtils.getProperty(item, "id").toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
